<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
 
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

function com_install() {
	global $mainframe, $database;
	
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_club` (
					`club_id` int(10) unsigned NOT NULL auto_increment,
					`club_name` varchar(64) NOT NULL default '',
					`club_text` text NOT NULL,
					`club_founded` varchar(16) NOT NULL default '',
					`club_members` int(10) unsigned NOT NULL default '0',
					`club_colors` varchar(32) NOT NULL default '',
					`club_logo` varchar(32) NOT NULL default '',
					`club_address` varchar(64) NOT NULL default '',
					`club_zipcode` varchar(8) NOT NULL default '',
					`club_city` varchar(32) NOT NULL default '',
					`club_country` varchar(3) NOT NULL default '',
					`club_internet` varchar(64) NOT NULL default '',
					`club_phone1` varchar(32) NOT NULL default '',
					`club_phone2` varchar(32) NOT NULL default '',
					`stadium_title` varchar(32) NOT NULL default '',
					`stadium_address` varchar(64) NOT NULL default '',
					`stadium_zipcode` varchar(8) NOT NULL default '',
					`stadium_city` varchar(32) NOT NULL default '',
					`stadium_capacity` int(10) unsigned NOT NULL default '0',
					`stadium_gmap_lat` varchar(16) NOT NULL default '',
					`stadium_gmap_lng` varchar(16) NOT NULL default '',
					`created` timestamp NOT NULL default CURRENT_TIMESTAMP,
					`created_by` int(10) unsigned NOT NULL default '0',
					`published` tinyint(3) unsigned NOT NULL default '0',
					PRIMARY KEY  (`club_id`),
					UNIQUE KEY `club_name` (`club_name`)
				) TYPE=MyISAM;";
	
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_event` (
					`event_id` int(10) unsigned NOT NULL auto_increment,
					`event_match` int(10) unsigned NOT NULL default '0',
					`event_playermap` int(10) unsigned NOT NULL default '0',
					`event_minute` int(10) unsigned NOT NULL default '0',
					`event_type` int(10) unsigned NOT NULL default '0',
					`event_text` text NOT NULL,
					`event_text_short` varchar(128) NOT NULL default '',
					PRIMARY KEY  (`event_id`)
				) TYPE=MyISAM;";
				
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_eventtype` (
					`eventtype_id` int(10) unsigned NOT NULL auto_increment,
					`eventtype_name` varchar(16) NOT NULL default '',
					`eventtype_title` varchar(16) NOT NULL default '',
					`playermap_association` varchar(32) NOT NULL default '',
					`ordering` int(11) NOT NULL default '0',
					`eventtype_icon` varchar(32) NOT NULL default '',
					PRIMARY KEY  (`eventtype_id`)
				) TYPE=MyISAM;";
	
	$queries[] = "INSERT INTO `#__leagueman_eventtype` 
						(`eventtype_id`, `eventtype_name`, `eventtype_title`, `playermap_association`, `ordering`, `eventtype_icon`)
				  VALUES 
				  		(1, 'Matches', 'Anz Spiele', 'cnt_matches', 1, 'cnt_matches.gif'),
						(2, 'Goal', 'Tor', 'cnt_goals', 2, 'cnt_goals.gif'),
						(3, 'Own Goal', 'Eigentor', 'cnt_own_goals', 3, ''),
						(4, 'Penalty', 'Elfmeter', 'cnt_penalties', 4, ''),
						(5, 'Assist', 'Assist', 'cnt_assists', 5, ''),
						(6, 'Get In', 'Einwechslung', 'cnt_in', 6, ''),
						(7, 'Get Out', 'Auswechslung', 'cnt_out', 7, ''),
						(8, 'Yellow Card', 'Gelbe Karte', 'cnt_yellow', 8, 'cnt_yellow.gif'),
						(9, 'YR Card', 'Gelb-Rote Karte', 'cnt_yellow_red', 9, 'cnt_yellow_red.gif'),
						(10, 'Red Card', 'Rote Karte', 'cnt_red', 10, 'cnt_red.gif');";
	
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_fanbase` (
					`user` int(10) unsigned NOT NULL default '0',
					`project` int(10) unsigned NOT NULL default '0',
					`team` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`user`,`project`)
				) TYPE=MyISAM;";
				
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_league` (
					`league_id` int(10) unsigned NOT NULL auto_increment,
					`league_name` varchar(32) NOT NULL default '',
					`ordering` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`league_id`)
				) TYPE=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_lineup` (
					`lineup_match` int(10) unsigned NOT NULL default '0',
					`lineup_team` int(10) unsigned NOT NULL default '0',
					`lineup_player` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`lineup_match`,`lineup_team`,`lineup_player`)
				) TYPE=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_match` (
					`match_id` int(10) unsigned NOT NULL auto_increment,
					`match_team1` int(10) unsigned NOT NULL default '0',
					`match_team2` int(10) unsigned NOT NULL default '0',
					`match_result_0_1` smallint(6) default NULL,
					`match_result_0_2` smallint(6) default NULL,
					`match_result_1_1` smallint(6) default NULL,
					`match_result_1_2` smallint(6) default NULL,
					`match_result_2_1` smallint(6) default NULL,
					`match_result_2_2` smallint(6) default NULL,
					`match_result_3_1` smallint(6) default NULL,
					`match_result_3_2` smallint(6) default NULL,
					`match_result_4_1` smallint(6) default NULL,
					`match_result_4_2` smallint(6) default NULL,
					`match_result_5_1` smallint(6) default NULL,
					`match_result_5_2` smallint(6) default NULL,
					`match_result_type` tinyint(4) unsigned NOT NULL default '0',
					`match_result_info` varchar(124) NOT NULL default '',
					`match_project` int(10) unsigned NOT NULL default '0',
					`match_round` int(10) unsigned NOT NULL default '0',
					`match_group` varchar(8) NOT NULL default '',
					`match_venue` varchar(64) NOT NULL default '',
					`match_date` date NOT NULL default '0000-00-00',
					`match_time` time NOT NULL default '00:00:00',
					`match_info_flag` tinyint(3) unsigned NOT NULL default '0',
					`published` tinyint(3) unsigned NOT NULL default '0',
					`match_crowd` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`match_id`),
					KEY `pro_round_pub` (`match_project`,`match_round`,`published`)
				) Type=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_player` (
					`player_id` int(10) unsigned NOT NULL auto_increment,
					`player_name` varchar(32) NOT NULL default '',
					`player_name_first` varchar(32) NOT NULL default '',
					`player_name_last` varchar(32) NOT NULL default '',
					`player_born` date NOT NULL default '0000-00-00',
					`player_died` date NOT NULL default '0000-00-00',
					`player_country` varchar(20) NOT NULL default '',
					`player_weight` int(10) unsigned NOT NULL default '0',
					`player_height` int(10) unsigned NOT NULL default '0',
					`player_text` text NOT NULL,
					`player_image` varchar(32) NOT NULL default '',
					`player_is_referee` tinyint(3) unsigned NOT NULL default '0',
					`player_is_trainer` tinyint(3) unsigned NOT NULL default '0',
					`created` datetime NOT NULL default '0000-00-00 00:00:00',
					`created_by` int(11) NOT NULL default '0',
					`player_state` tinyint(3) unsigned NOT NULL default '0',
					PRIMARY KEY  (`player_id`)
				) TYPE=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_playermap` (
					`playermap_id` int(10) unsigned NOT NULL auto_increment,
					`playermap_player` int(10) unsigned NOT NULL default '0',
					`playermap_team` int(10) unsigned NOT NULL default '0',
					`playermap_project` int(10) unsigned NOT NULL default '0',
					`position_number` int(11) NOT NULL default '0',
					`playermap_position` int(10) unsigned NOT NULL default '0',
					`playermap_image` varchar(196) NOT NULL default '',
					`cnt_matches` smallint(5) unsigned NOT NULL default '0',
					`cnt_goals` smallint(5) unsigned NOT NULL default '0',
					`cnt_own_goals` smallint(5) unsigned NOT NULL default '0',
					`cnt_penalties` smallint(5) unsigned NOT NULL default '0',
					`cnt_assists` smallint(5) unsigned NOT NULL default '0',
					`cnt_in` smallint(5) unsigned NOT NULL default '0',
					`cnt_out` smallint(5) unsigned NOT NULL default '0',
					`cnt_yellow` smallint(5) unsigned NOT NULL default '0',
					`cnt_yellow_red` smallint(5) unsigned NOT NULL default '0',
					`cnt_red` smallint(5) unsigned NOT NULL default '0',
					PRIMARY KEY  (`playermap_id`),
					UNIQUE KEY `ptp` (`playermap_player`,`playermap_team`,`playermap_project`)
				) TYPe=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_position` (
					`position_id` int(10) unsigned NOT NULL auto_increment,
					`position_name` varchar(16) NOT NULL default '',
					`position_projecttype` varchar(32) NOT NULL default '',
					`ordering` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`position_id`)
				) TYPE=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `jos_leagueman_project` (
					`project_id` int(10) unsigned NOT NULL auto_increment,
					`project_name` varchar(32) NOT NULL default '',
					`project_category` int(11) unsigned NOT NULL default '0',
					`project_season` int(11) unsigned NOT NULL default '0',
					`project_template` varchar(32) NOT NULL default '0',
					`eventtypes` text NOT NULL,
					`project_admin` int(11) unsigned NOT NULL default '0',
					`project_cur_round` int(11) unsigned NOT NULL default '0',
					`project_min_round` smallint(6) NOT NULL default '0',
					`project_max_round` smallint(6) NOT NULL default '0',
					`published` tinyint(3) unsigned NOT NULL default '0',
					`finished` tinyint(3) unsigned NOT NULL default '0',
					`params` text NOT NULL,
					PRIMARY KEY  (`project_id`),
					KEY `project_category` (`project_category`)
				) Type=MyISAM;";
				
	$queries[] = "ALTER TABLE `#__leagueman_project` 
					CHANGE `project_category` `project_league` INT( 11 ) UNSIGNED NOT NULL DEFAULT '0';";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_projecttype` (
					`type_id` int(10) unsigned NOT NULL auto_increment,
					`type_name` varchar(32) NOT NULL default '',
					`eventtypes` text NOT NULL,
					`type_template` varchar(32) NOT NULL default '',
					PRIMARY KEY  (`type_id`),
					UNIQUE KEY `type_name` (`type_name`),
					UNIQUE KEY `type_template` (`type_template`)
				) TYPE=MyISAM;";

	$queries[] = "INSERT INTO `#__leagueman_projecttype` VALUES (1, 'Football', '', 'football');";
	$queries[] = "INSERT INTO `#__leagueman_projecttype` VALUES (2, 'Icehockey', '', 'icehockey');";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_round` (
					`round_id` int(10) unsigned NOT NULL auto_increment,
					`round_name` varchar(32) NOT NULL default '',
					`round_name_short` varchar(3) NOT NULL default '',
					`round_project` int(11) unsigned NOT NULL default '0',
					`ordering` int(11) unsigned NOT NULL default '0',
					`content_id` int(11) unsigned NOT NULL default '0',
					`hits` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`round_id`),
					KEY `round_project` (`round_project`)
				) TYPE=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_season` (
					`season_id` int(10) unsigned NOT NULL auto_increment,
					`season_name` varchar(32) NOT NULL default '',
					`ordering` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`season_id`)
				) TYPE=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_team` (
					`team_id` int(10) unsigned NOT NULL auto_increment,
					`team_club` int(10) unsigned NOT NULL default '0',
					`team_name` varchar(32) NOT NULL default '',
					`team_name_short` varchar(16) NOT NULL default '',
					`team_info` varchar(32) NOT NULL default '',
					`team_logo` varchar(192) NOT NULL default '',
					`team_text` text NOT NULL,
					PRIMARY KEY  (`team_id`)
				) TYPE=MyISAM;";

	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_teammap` (
					`teammap_id` int(10) unsigned NOT NULL auto_increment,
					`teammap_project` int(10) unsigned NOT NULL default '0',
					`teammap_team` int(10) unsigned NOT NULL default '0',
					`teammap_points` smallint(6) NOT NULL default '0',
					`teammap_user` int(10) unsigned NOT NULL default '0',
					`teammap_trainer` int(10) unsigned NOT NULL default '0',
					`teammap_info` varchar(64) NOT NULL default '',
					`teammap_image` varchar(192) NOT NULL default '',
					`teammap_text` text NOT NULL,
					PRIMARY KEY  (`teammap_id`),
					UNIQUE KEY `project_team` (`teammap_project`,`teammap_team`),
					KEY `teammap_user` (`teammap_user`)
				) TYPE=MyISAM;";
				
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_tipgroup` (
					`tipgroup_id` int(10) unsigned NOT NULL auto_increment,
					`tipgroup_name` varchar(32) NOT NULL default '',
					`tipgroup_text` text NOT NULL,
					`tipgroup_project` int(10) unsigned NOT NULL default '0',
					`created` datetime NOT NULL default '0000-00-00 00:00:00',
					`created_by` int(10) unsigned NOT NULL default '0',
					`created_by_alias` varchar(32) NOT NULL default '',
					`published` tinyint(3) unsigned NOT NULL default '0',
					`params` text NOT NULL,
					PRIMARY KEY  (`tipgroup_id`)
				) TYPE=MyISAM;";
	
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_tipjoker` (
					`tipjoker_group` int(10) unsigned NOT NULL default '0',
					`tipjoker_match` int(10) unsigned NOT NULL default '0',
					PRIMARY KEY  (`tipjoker_group`,`tipjoker_match`)
				) TYPE=MyISAM";
	
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_tipresult` (
					`tipresult_group` int(10) unsigned NOT NULL default '0',
					`tipresult_user` int(10) unsigned NOT NULL default '0',
					`tipresult_match` int(10) unsigned NOT NULL default '0',
					`created` timestamp NOT NULL default CURRENT_TIMESTAMP,
					`tipresult_1` smallint(5) unsigned default NULL,
					`tipresult_2` smallint(5) unsigned default NULL,
					`tipresult_toto` tinyint(3) unsigned default NULL,
					PRIMARY KEY  (`tipresult_group`,`tipresult_user`,`tipresult_match`)
				) TYPE=MyISAM";
	
	$queries[] = "CREATE TABLE IF NOT EXISTS `#__leagueman_tipuser` (
					`tipgroup_id` int(10) unsigned NOT NULL default '0',
					`user_id` int(10) unsigned NOT NULL default '0',
					`register_timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
					`approved` tinyint(3) unsigned NOT NULL default '0',
					PRIMARY KEY  (`tipgroup_id`,`user_id`)
				) TYPE=MyISAM";
	
	foreach($queries AS $query){
		$database->setQuery($query);
		$database->query();
	}
	
	$abs = $mainframe->getCfg("absolute_path");
	$image_path = $abs . "/images/stories/leagueman";
	
	mosMakePath($image_path);
	
	$dirs = array("logos", "players", "teams", "eventtypes", "contacts");
	
	foreach($dirs AS $dir){
		if(mosMakePath($image_path . "/" . $dir)){
			echo $image_path . "/" . $dir . " created successfully!<br />";
		} else {
			echo "<span style=\"color:red;\">Could not create Imagedir <b>" . $image_path . "/" . $dir . "</b></span><br />";
		}
	}
	
	copy($abs . "/components/com_leagueman/images/goals.gif", 		$image_path . "/eventtypes/cnt_goals.gif");
	copy($abs . "/components/com_leagueman/images/red.gif", 		$image_path . "/eventtypes/cnt_red.gif");
	copy($abs . "/components/com_leagueman/images/yellow.gif", 		$image_path . "/eventtypes/cnt_yellow.gif");
	copy($abs . "/components/com_leagueman/images/yellow_red.gif", 	$image_path . "/eventtypes/cnt_yellow_red.gif");
	copy($abs . "/components/com_leagueman/images/matches.gif",		$image_path . "/eventtypes/cnt_matches.gif");
	
	install_module("Next. vs",			"mod_leagueman_next_vs");
	install_module("Leaguetable",		"mod_leagueman_leaguetable");
	install_module("Popular Teams",		"mod_leagueman_pop_teams");
	install_module("Random Player",		"mod_leagueman_random_player");
	
	install_plugin("Leaguetable",		"leagueman_leaguetable");
	install_plugin("RoundresultNav",	"leagueman_roundresultnav");
	install_plugin("Roundresult",		"leagueman_roundresult");
	install_plugin("Teamresult",		"leagueman_teamresult");
	install_plugin("Clubinfo",			"leagueman_clubinfo");
	install_plugin("History",			"leagueman_history");
	install_plugin("Related Items",		"leagueman_related_items");
	install_plugin("Squad",				"leagueman_squad");
	install_plugin("Discussion Link",	"leagueman_discussion_link");
	install_plugin("Tipgroups",			"leagueman_tipgroups");
	
	echo "Leagueman installed successfully<br />
	Visit <a href=\"http://leagueman.joomlabasic.com\" target=\"blank\">http://leagueman.joomlabasic.com</a> for more.";
}

function install_module($name, $element, $position = "right"){
	global $mainframe, $database;
	
	$database->setQuery("SELECT COUNT(id) FROM #__modules WHERE module = " . $database->Quote($element));
	if($database->loadResult() == 0){
		$row = new mosModule( $database );
		$row->title 		= $name;
		$row->ordering 		= 99;
		$row->published		= 0;
		$row->position 		= $position;
		$row->showtitle 	= 1;
		$row->iscore 		= 0;
		$row->access 		= 0;
		$row->client_id 	= 0;
		$row->module 		= $element;

		$row->store();
	}
	
	$abs = $mainframe->getCfg("absolute_path");
	
	copy($abs . "/components/com_leagueman/modules/$element.php", $abs . "/modules/$element.php");
	copy($abs . "/components/com_leagueman/modules/$element.xml", $abs . "/modules/$element.xml");
}

function install_plugin($name, $element){
	global $mainframe, $database;
	
	$database->setQuery("SELECT COUNT(id) FROM #__mambots WHERE element = " . $database->Quote($element));
	if($database->loadResult() == 0){
		$row = new mosMambot( $database );
		$row->name 		= $name;
		$row->ordering 	= 0;
		$row->folder 	= 'content';
		$row->iscore 	= 0;
		$row->access 	= 0;
		$row->client_id = 0;
		$row->element 	= $element;
		$row->published	= 1;

		$row->store();
	}
	
	$abs = $mainframe->getCfg("absolute_path");
	
	copy($abs . "/components/com_leagueman/plugins/$element.php", $abs . "/mambots/content/$element.php");
	copy($abs . "/components/com_leagueman/plugins/$element.xml", $abs . "/mambots/content/$element.xml");
}

?>